﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AB_SD_Entities;
using AB_SD_Commons.Helpers;
using System.Data.SqlClient;
using System.Data;

namespace AB_SD_DataAccess
{
    public static class UserDAO
    {
        /// <summary>
        /// Select a user by username
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static User Select(string username)
        {
            User result = new User();
            
            string sql = "select * from users where username=@user";
            SqlParameter[] sqlparams = new SqlParameter[1];
            sqlparams[0] = new SqlParameter("@user", SqlDbType.VarChar);
            sqlparams[0].Value = username;
            DataTable dt = SqlHelper.ExecuteSelectQuery(sql, sqlparams);

            if (dt != null)
            {
                if (dt.Rows.Count == 1)
                {
                    result.Username = dt.Rows[0]["Username"].ToString();
                    result.Password = dt.Rows[0]["Password"].ToString();
                    result.Email = dt.Rows[0]["Email"].ToString();
                    result.IsSuper = (bool)dt.Rows[0]["IsSuperUser"];
                    return result;
                }
                else
                    return null;
            }
            else
                return null;
        }
    }
}
